<template>
	<view class="box">
		<!-- banner 广告位 -->
		<view class="secondary-banner">
			<image src="../static/banner3.jpg"></image>
		</view>
		<!--  #ifdef  MP-QQ -->
		<button class="secondary-print flex just-between align-center" open-type="getUserInfo"
			@click="chooseMessageFile(0)">
			<view class="secondary-img">
				<image src="https://oss.xhm2006.com/Public/QQApplet/qq.png" style="width:70rpx;height:82rpx;"></image>
			</view>
			<view class="">
				<!-- 标题 -->
				<view class="secondary-title Bold">
					选择QQ文档打印
				</view>
				<!-- 副标题 -->
				<view class="secondary-futitle Medium">
					从QQ聊天记录中选择文档打印
				</view>
			</view>
			<!-- 图片 -->
			<view class="secondary-jt">
				<image src="https://oss.xhm2006.com/Public/QQApplet/into@2x.png"></image>
			</view>
		</button>
		<!--  #endif -->

		<!--  #ifdef  MP-WEIXIN -->
		<view class="secondary-print flex just-between align-center" @click="chooseMessageFile(0)">
			<view class="secondary-img">
				<image src="../static/wechat1@2x.png"></image>
			</view>
			<view class="">
				<!-- 标题 -->
				<view class="secondary-title Bold">
					选择微信文档打印
				</view>
				<!-- 副标题 -->
				<view class="secondary-futitle Medium">
					从微信聊天记录中选择文档打印
				</view>
			</view>
			<!-- 图片 -->
			<view class="secondary-jt">
				<image src="../static/into@2x.png"></image>
			</view>
		</view>
		<!--  #endif -->
		<!-- 打印在A4纸上 -->

		<!--  #ifdef  MP-QQ -->
		<button class="secondary-print flex just-between align-center" @click="chooseMessageFile(1)"
			open-type="getUserInfo">
			<view class="" style="width: 82rpx;height: 74rpx;">
				<image src="../static/photo@2x.png"></image>
			</view>
			<view class="" style="margin-left: -154rpx;">

				<view class="secondary-title Bold">
					手机图片打印
				</view>

				<view class="secondary-futitle Medium">
					将图片打印到A4纸上
				</view>
			</view>

			<view class="secondary-jt">
				<image src="../static/into@2x.png"></image>
			</view>
		</button>

		<!--#endif -->
		<!--  #ifdef  MP-WEIXIN -->
		<view class="secondary-print flex just-between align-center" @click="chooseMessageFile(1)">
			<view class="" style="width: 82rpx;height: 74rpx;">
				<image src="../static/photo@2x.png"></image>
			</view>
			<view class="" style="margin-left: -154rpx;">

				<view class="secondary-title Bold">
					手机图片打印
				</view>

				<view class="secondary-futitle Medium">
					将图片打印到A4纸上
				</view>
			</view>

			<view class="secondary-jt">
				<image src="../static/into@2x.png"></image>
			</view>
		</view>
		<!--#endif -->
		<!--上传成功后的弹窗 -->
		<x-u-popu :lengthchange="lengthchange" :lengtherror="lengtherror" v-if="show">
			<template slot="content">
				{{titlesshow}}
			</template>
			<template slot="contentItem">
				{{titleItem}}
			</template>
		</x-u-popu>
	</view>
</template>
<script>
	import {
		A4fail,
		wxfile
	} from '@/utils/facility/documentPrinting.js'

	// 判断是否登录权限 登陆后吧获取的unicodeid在文件上传的时候发给后端
	import {
		login
	} from "@/utils/facility/login.js"
	// 文件上传服务器
	import {
		filees,
		documents,
		applefiles
	} from '@/api/facility/documentPrinting.js'
	export default {

		data() {
			return {
				Refused: true, // 上传成功还是失败都不可以再次点击上传
				show: false, //控制显示上传成功后的弹窗
				titlesshow: '', //上传成功失败的提示
				titleItem: '', //上传成功失败的之后的下一步操作
				pathError: 0, //上传失败的张数
				pathChange: 0, //上传成功的张数
				// messageList: '', //微信文件打印资源 
				messageListWx: '', //微信文件打印资源 jsonparse的文件
				showd: '',
				orderId: '', //订单id
				urlq: '', //传输的完整的二维码链接
				// 上传多个   6.13新增  递归索引传输
				bachIndex: 0, //上传文档的索引
				unionId: '', //获取本地的openid 其实是unionId

			}
		},
		onShow() {
			this.unionId = uni.getStorageSync('openid');
		},
		onLoad(options) {
			// if (options.q) {
			// 	var scan_url = decodeURIComponent(options.q);
			var scan_url = "https://api.cloudmis.cn?id='6'&orderId=7A27DB698ED24D0DBE7EB3E935AB44F0&specId=0";
			var reg = /[a-z]+:\/\/([A-Za-z0-9.]+)+\?(([a-z])+=([A-Za-z0-9']+)+)+\&([A-Za-z0-9]+)+=([A-Za-z0-9']+)/i;
			var arr = scan_url.match(reg);
			console.log(arr[6], 'arr[6]')
			this.orderId = arr[6]
			this.urlq = options.q
			// }

		},
		methods: {
			chooseMessageFile(num) {
				if (num == 0) {
					// if (this.Refused) { //上传成功还是失败都不可以再次点击上传
					// 	var value = uni.getStorageSync('openid');
					if (this.unionId == '') {
						login().then(res => {
							this.file();
						})
					} else {
						this.file(); // 获取本地的文件
					}
					// }
				}
				if (num == 1) {
					if (this.unionId == '') {
						login().then(res => {
							this.A4img();
						})
					} else {
						this.A4img(); // 获取本地的文件
					}
				}
			},
			// 获取本地的文件
			A4img() {
				A4fail(6).then(res => {
					if (typeof res == 'string' && res != undefined) {
						this.messageListWx = JSON.parse(res)
						uni.showLoading({
							title: '上传中...'
						});
						// 封装发送文档上传请求 
						this.A4uploadFail()
					}
				})
			},
			file() {
				// 获取本地的文件
				wxfile(9).then(res => {
					if (typeof res == 'string' && res != undefined) {
						this.messageListWx = JSON.parse(res)
						uni.showLoading({
							title: '上传中...'
						});

						this.uploadFail()

					}
				}).catch(error => {
					console.log('取消上传')
				})
			},
			uploadFail() {
				filees({
					// 上传多个   6.13新增  递归索引传输
					filePath: this.messageListWx.pathList[this.bachIndex].path,
					// filePath: this.messageListWx.path,
					postdata: [{
						fileId: 'unionId', //unicode
						value: this.unionId
					}, {
						fileId: 'orderId',
						value: this.orderId, //顶单id
					}, {
						fileId: 'name', //unicode 
						value: this.messageListWx.pathList[this.bachIndex].name,
						// value: this.messageListWx.name
					}]
				}).then(res => {
					if (res.success == true) {
						this.bachIndex++;
						this.pathChange++;
						if (this.bachIndex != this.messageListWx.pathList.length) {
							this.uploadFail();
						}
						if (this.bachIndex == this.messageListWx.pathList.length) {
							var text1 = [{
								fileId: 'orderId',
								value: this.orderId
							}, {
								fileId: 'unionId',
								value: this.unionId
							}];
							documents(text1).then(res => {
								this.trycatch(`上传成功${this.pathChange}个，失败${this.pathError}个`,
									"请点击确定后操作打印设备打印")
							}).catch(error => {
								this.trycatch("上传失败服务器异常", "请点击确定后退出重试")
							})
						}
					} else if (res.success == false) {
						this.bachIndex++;
						this.pathError++;
						if (this.bachIndex != this.messageListWx.pathList.length) {
							this.uploadFail();
						}
						if (this.bachIndex == this.messageListWx.pathList.length) {
							this.trycatch(`上传成功${this.pathChange}个，失败${this.pathError}个`, "请点击确定后退出重试")
						}
					}
				}).catch(error => {
					this.trycatch("上传失败服务器异常", "请点击确定后退出重试")
				})
			},
			// A4纸张
			A4uploadFail() {
				for (let i = 0; i < this.messageListWx.pathList.length; i++) {
					applefiles({
						filePath: this.messageListWx.pathList[i].path,
						postdata: [{
							fileId: 'unionId', //unicode
							value: this.unionId
						}, {
							fileId: 'orderId',
							value: this.orderId, //顶单id
						}, {
							fileId: 'name', //unicode 
							value: 'A4纸打印'
						}, {
							fileId: "isPreview",
							value: true
						}]
					}).then(res => {
						if (res.success == true) {
							this.bachIndex++;
							this.pathChange++;
							if (this.bachIndex == this.messageListWx.pathList.length) {
								var text1 = [{
									fileId: 'orderId',
									value: this.orderId
								}, {
									fileId: 'unionId',
									value: this.unionId
								}];
								documents(text1).then(res => {
									this.trycatch(`上传成功${this.pathChange}个，失败${this.pathError}个`,
										"请点击确定后操作打印设备打印")
								}).catch(error => {
									this.trycatch("上传失败服务器异常", "请点击确定后退出重试")
								})
							}
						} else if (res.success == false) {
							this.bachIndex++;
							this.pathError++;
							if (this.bachIndex == this.messageListWx.pathList.length) {
								this.trycatch(`上传成功${this.pathChange}个，失败${this.pathError}个`, "请点击确定后退出重试")
							}
						}
					})
				}
			},
			trycatch(name1, name2) {
				this.Refused = false // 上传成功还是失败都不可以再次点击上传
				uni.hideLoading()
				if (this.pathChange == 0) {
					this.titlesshow = name1
					this.titleItem = '请点击确定后退出重试'
					this.show = true
				} else {
					this.titlesshow = name1
					this.titleItem = name2
					this.show = true
					this.pathError = 0 //上传失败的张数
					this.pathChange = 0 //上传成功的张数
				}

			}
		}
	}
</script>
<style>
	.secondary-jt {
		width: 20rpx;
		height: 36rpx;
	}

	.secondary-title {
		display: flex;
		justify-content: left;
		font-size: 31rpx;
		color: #222222;
		margin-bottom: 24rpx;
		/* font-weight: bold; */
	}

	.secondary-futitle {
		font-size: 31rpx;
		color: #999999;
		font-weight: 400;
	}

	.secondary-img {
		width: 82rpx;
		height: 74rpx;
		margin-right: -32rpx;
	}

	.secondary-print {
		height: 220rpx;
		/* padding: 40rpx 48rpx 42rpx 40rpx; */
		padding: 70rpx 40rpx 70rpx 30rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(230, 230, 230, 0.5);
		border-radius: 20rpx;
		margin-bottom: 44rpx;
		border: 2px solid #38FF38;
	}

	.box {
		width: 100%;
		padding: 30rpx 30rpx 0rpx 30rpx
	}

	.secondary-imglogo {
		width: 70rpx;
		height: 87rpx;
		margin-right: 34rpx;
		/* border: 2rpx solid #000000; */
	}

	.secondary-banner {
		height: 370rpx;
		background: linear-gradient(107deg, #5B4FFC, #7F49F9);
		box-shadow: 0px 3px 5px 0px rgba(106, 76, 251, 0.51);
		border-radius: 10px;
		margin-bottom: 40rpx
	}

	.secondary-print {
		height: 220rpx;
		/* padding: 40rpx 48rpx 42rpx 40rpx; */
		padding: 70rpx 40rpx 70rpx 30rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(230, 230, 230, 0.5);
		border-radius: 20rpx;
		margin-bottom: 44rpx;
		border: 2px solid #38FF38;
	}




	/* .secondary-img {
		 
		width: 18rpx;
		height: 35rpx;
	 

	} */
</style>
